package com.hsmpay.mobile.dao.trans.impl;

import com.hsmpay.common.dao.Impl.BaseDaoSupport;
import com.hsmpay.common.pojo.user.User;
import com.hsmpay.common.util.ClearMac;
import com.hsmpay.common.util.OrderTypeUtil;
import com.hsmpay.mobile.dao.trans.TransOrderDao;
import com.hsmpay.mobile.pojo.trans.TransOrder;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * 交易订单 sqlmap 接口 transOrderDao
 * 对应pojo TransOrder
 *
 * @author 颜铃璋
 *         创建时间：2013-1-21
 */
@Repository("transOrderDao")
public class TransOrderDaoImpl extends BaseDaoSupport<TransOrder, Long> implements TransOrderDao<TransOrder, Long> {
    /**
     * 根据订单ID修改状态
     *
     * @param orderNum
     * @param status
     * @param clearing
     * @return
     */
    public int updateStatusClearing(String orderNum, Integer status, Integer clearing) throws SQLException {
        TransOrder order = new TransOrder();
        order.setStatus(status);
        order.setOrderNum(orderNum);
        order.setClearing(clearing);
        Date date = new Date();
        order.setUpdateLongTime(date.getTime());

        if (null != status && 0 == status.intValue() && 6 == status.intValue()) {
            order.setBrokerage(0.00);
            order.setDealerBrokerage(0.00);
        }
        return getSqlMapClientTemplate().update("transOrderSqlMap.updateStatusClearing", order);
    }

    /**
     * 查出交易金额  总额
     *
     * @param transOrder
     * @return
     * @throws SQLException
     */
    public Double getTransMoneySum(TransOrder transOrder) throws SQLException {
        Double sum = (Double) getSqlMapClient().queryForObject("transOrderSqlMap.getTransMoneySum", transOrder);
        return null == sum ? 0.00 : sum;
    }

    /**
     * 修改签名
     *
     * @return
     * @throws SQLException
     */
    public int updateSignaturePathByNum(TransOrder transOrder) throws SQLException {
        return getSqlMapClientTemplate().update("transOrderSqlMap.updateSignaturePathByNum", transOrder);
    }

    /**
     * 修改渠道手续费
     *
     * @return
     * @throws SQLException
     */
    public int updateRevenueFeeMoneyByNum(TransOrder transOrder) throws SQLException {
        return getSqlMapClientTemplate().update("transOrderSqlMap.updateRevenueFeeMoneyByNum", transOrder);
    }

    /**
     * 修改提现订单号
     *
     * @param orderNum
     * @param cashOrderNum
     * @return
     * @throws SQLException
     */
    public int updateCashOrderByNum(String orderNum, String cashOrderNum) throws SQLException {
        TransOrder transOrder = new TransOrder();
        transOrder.setOrderNum(orderNum);
        transOrder.setCashOrderNum(cashOrderNum);
        return getSqlMapClientTemplate().update("transOrderSqlMap.updateCashOrderByNum", transOrder);
    }

    /**
     * 修改未结算的订单 为结算处理中
     * 在修改绑定银行卡 后  判断有没有订单是未结算状态
     * 如果是 并且 不收8毛钱的订单  那么就修改状态为 处理中  这样就可以进行结算合单  并且可以  结算了
     *
     * @return
     * @throws Exception
     */
    public int updateUnCOrderClearing(User user) throws SQLException {
        List<TransOrder> orderList = searchOrderListByUserId(user.getId(), 8);//查询未结算的 结算订单
//		double tatolMoney = getUnCOrderTransMoneySumByUserId(user.getId());
        for (int i = 0; i < orderList.size(); i++) {
            TransOrder o = orderList.get(i);

//			if(o.getTerminalTypeId().longValue() == 1 && o.getRoleId().longValue() == 2){//花生米 费率版  要收8毛 手续费
//				if(tatolMoney > o.getFeeMoney()){//不是 第一笔  都改掉
//					updateStatusClearing(o.getOrderNum(),2,null);//修改  订单状态为 2  就可以  进行结算合单了
//				}//小于 8毛的 
//				//修改8毛  第二笔  就不收 8毛手续费了
//				/*if(i != 0){
//					o.setFeeMoney(0.00);
//					o.setChargeAmount(o.getTransMoney());
//					updateOrderFeeMoney(o);
//				}*/
//			}else{
            updateStatusClearing(o.getOrderNum(), 2, null);
//			}

            if (StringUtils.isEmpty(o.getObjectiveCard())) {//如果订单银行卡为空 同步用户银行卡信息
                TransOrder order = new TransOrder();
                order.setOrderNum(o.getOrderNum());
                order.setObjectiveCard(user.getBankCard());//卡号
                order.setObjectiveName(user.getCardholder());//
                order.setObjectiveOpBank(user.getOpeningBank());//开户行
                order.setObjectiveBBProvince(user.getBBankProvince());//支行省
                order.setObjectiveBBCity(user.getBBankCity());//
                order.setObjectiveBBank(user.getBranchesBank());//支行
                order.setObjectiveBBankCode(user.getBranchesBankCode());//万三支行 代码
                order.setObjectiveAccBankCode(user.getAccBankCode());//三毛  代码
                order.setClearMac(ClearMac.getOrderClearMac(order.getOrderNum(), order.getUserId(), order.getTransMoney(), order.getObjectiveCard(), order.getObjectiveOpBank(), order.getObjectiveName()));
                updateObjectiveBankInfo(order);
            }
        }
        return 1;
    }

    /**
     * 根据userid 获取未结算订单列表
     *
     * @param userId
     * @param status 交易状态  0交易失败 1交易成功 2银行交易处理中 3 未付款 4等待对方付款 5关闭交易 6 交易已撤销 7 交易发起撤销 8 未结算
     * @return
     * @throws SQLException
     */
    public List<TransOrder> searchOrderListByUserId(Long userId, Integer status) throws SQLException {
        TransOrder order = new TransOrder();
        order.setClearing(0);//结算状态 未结算
        order.setStatus(status);//订单状态  未结算
        order.setOrderTypeId(OrderTypeUtil.CLEAR_MONEY);//订单类型 结算
        order.setUserId(userId);
        return searchEntityList(order);
    }

    /**
     * 根据userid 获取未结算订单 总金额
     *
     * @param userId
     * @return
     * @throws SQLException
     */
    public Double getUnCOrderTransMoneySumByUserId(Long userId) throws SQLException {
        TransOrder order = new TransOrder();
        order.setClearing(0);//结算状态 未结算
        order.setStatus(8);//订单状态  未结算
        order.setOrderTypeId(OrderTypeUtil.CLEAR_MONEY);//订单类型 结算
        order.setUserId(userId);
        return getTransMoneySum(order);
    }

    /**
     * 修改 目标银行卡信息
     *
     * @param order
     * @return
     * @throws SQLException
     */
    public int updateObjectiveBankInfo(TransOrder order) throws SQLException {
        return getSqlMapClientTemplate().update("transOrderSqlMap.updateObjectiveBankInfo", order);
    }

    /**
     * 查询安心一百订单号
     *
     * @return
     * @throws SQLException
     */
    public Integer searchAnXinYBTransId() throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchAnXinYBTransId");
    }

    /**
     * 查询辽宁银联订单号
     *
     * @return
     * @throws SQLException
     */
    public Integer searchLiaoNingTransId() throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchLiaoNingTransId");
    }

    /**
     * 查询宁波银联订单号
     *
     * @return
     * @throws SQLException
     */
    public Integer searchNingBoTransId() throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchNingBoTransId");
    }

    /**
     * 查询盛付通
     *
     * @return
     * @throws SQLException
     */
    public Integer searchShengFTTransId() throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchShengFTTransId");
    }

    /**
     * ECB 交易
     *
     * @return
     * @throws SQLException
     */
    public Integer searchECBTransId() throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchECBTransId");
    }

    /**
     * 瑞升 交易
     *
     * @return
     * @throws SQLException
     */
    public Integer searchRuiShengTransId() throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchRuiShengTransId");
    }

    /**
     * 瑞升国通交易 流水
     * @return
     * @throws SQLException
     */
    public Integer searchRuiShengGTTransId()throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchRuiShengGTTransId");
    }

    /**
     * posp 交易
     *
     * @return
     * @throws SQLException
     */
    public Integer searchPospTransId() throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchPospTransId");
    }

    /**
     * EPoint交易流水
     *
     * @return
     * @throws SQLException
     */
    public Integer searchEPointTransId() throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchEPointTransId");
    }

    /**
     * 瑞晟国通
     * @return
     * @throws SQLException
     */
    public Integer searchGuoTongTransId() throws SQLException {
        return null;
    }

    /**
     * E点支付
     * @return
     * @throws SQLException
     */
    public Integer searchIdeaioeTransId()throws SQLException{
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchIdeaioeTransId");
    }

    /**
     * 优银支付
     * @return
     * @throws SQLException
     */
    public Integer searchUinPayTransId() throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchUinPayTransId");
    }

    /**
     * 银生宝支付
     * @return
     * @throws SQLException
     */
    public Integer searchYinShengBaoTransId() throws SQLException {
        return (Integer) getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchYinShengBaoTransId");
    }

    /**
     * pos支付
     * @return
     * @throws SQLException
     */
    public Integer searchPOSTransId() throws SQLException {
        return (Integer)getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchPOSTransId");
    }

    /**
     * 支付通交易流水
     * @return
     * @throws SQLException
     */
    public Integer searchZHIFUTONGTransId() throws SQLException {
        return (Integer)getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchZHIFUTONGTransId");
    }

    /**
     * 更新机构号
     * @param transOrder
     * @return
     * @throws SQLException
     */
    public int updateOrganization(TransOrder transOrder) throws SQLException {
        return getSqlMapClientTemplate().update("transOrderSqlMap.updateOrganizationId", transOrder);
    }

    /**
     * 翰鑫交易流水
     * @return
     * @throws SQLException
     */
    public Integer searchHanXinTransId() throws SQLException {
        return (Integer)getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchHanXinTransId");
    }

    /**
     * 中岚交易流水
     * @return
     * @throws SQLException
     */
    public Integer searchZhongLanTransId() throws SQLException {
        return (Integer)getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchZhongLanTransId");
    }

    /**
     * 卡银卡交易流水
     * @return
     * @throws SQLException
     */
    public Integer searchKaYinKaTransId() throws SQLException {
        return (Integer)getSqlMapClientTemplate().queryForObject("transOrderSqlMap.searchKaYinKaTransId");
    }
}
